<?php

namespace app\admin\controller;


use app\common\model\OrderShoufei;
use app\common\model\Manager;
use app\common\model\Xueyuan;
use app\common\model\XueyuanJiaofei;
use think\Db;

class Shoufei extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取信息
     * @return void
     */
    public function list()
    {
        //查询条件
        $where['sid'] = input('id');

        $manager = Manager::getColumn([],'id,email');

        //获取分页列表
        $list = OrderShoufei::getAll($where,'id desc');

        if(!$list){
            $list=[];
        }

        //返回信息
        return returnJson(0, '', compact('list','manager'));
    }

    /**
     * 分页获取信息
     * @return void
     */
    public function index()
    {
        //查询条件
        $where = array();

        if (input('checked')!=='') {
            $where['checked'] = input('checked');
        }
        if (input('sid')) {
            $where['sid'] = array('eq', input('sid'));
        }
        if (input('mid')) {
            $where['mid'] = array('eq', input('mid'));
        }
        if (input('get.time/a')[0]) {
            $where['update_time'] = array(
                array('>=',strtotime(input('get.time/a')[0])),
                array('<=',strtotime(input('get.time/a')[1]))
            );
        }

        //分页信息
        $page = max(input('page'), 1);
        $size = input('limit',20);
        //获取分页列表
        $list = OrderShoufei::getPage($where, $page, $size);
        //总页码
        $total = OrderShoufei::getCount($where);
        //数据返回
        if (!$list) {
            return returnJson(0, '', array('list' => [], 'total' => 0));
        }
        $manager = Manager::getColumn([],'id,email');
        foreach ($list as $k=>$v){
            $list[$k]['manager_name'] = isset($manager[$v['mid']])?$manager[$v['mid']]:'';
        }


        return returnJson(0, '', [
            'list' => $list,
            'size' => $size,
            'total' => $total
        ]);
    }

    /**
     * 创建收费记录
     * @return \think\response\Json
     */
    public function create()
    {
        //表单信息
        $data = input('');

        //验证表单
        if (!$data['money']) {
            return returnJson(202, '请填写收费金额');
        }

        //获取已消课时
        $now_jid = Xueyuan::getValue(array('id'=>array('eq',$data['sid'])),'now_jid');
        $shoufei = [];
        if($now_jid){
            $shoufei = OrderShoufei::getOne(array('id'=>array('eq',$now_jid)));
        }

        //更新数据
        $save_data = [
            'money' => $data['money'],
            'sid' => $data['sid'],
            'has_class_hour' => $shoufei?$shoufei['has_class_hour']:0,
            'order_sn' => create_order_sn($data['sid'], 'SF'),
            'mid' => $data['mid'],
            'update_time' => time(),
            'create_time' => time(),
        ];
        OrderShoufei::setInsertGetId($save_data);

        //返回信息
        return returnJson(0, '');
    }


    /**
     * 交易关闭
     * @return \think\response\Json
     */
    public function guanbi()
    {
        //查询条件
        $where = array(
            'id' => input('id')
        );

        //更新数据
        $save = array(
            'checked'=>2,
            'update_time' => time()
        );

        //更新
        OrderShoufei::update($save,$where);

        //返回信息
        return returnJson(0, '');
    }

}
